﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Drawing.Printing;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows.Forms;
using XCI.Windows.Helper;
using System.Windows.Forms;

namespace XCI.Sys.Forms
{
    public partial class FrmReportEdit : FrmBase
    {
        private bool created;
        private SysReport model;
        //private string localPath;

        public FrmReportEdit()
        {
            InitializeComponent();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keys)
        {
            return OnEsc(msg, keys, () => { EditFormEscResultCancel(); });
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void InitForm()
        {
            BindPrinterList();
            BindModel();
        }

        /// <summary>
        /// 设置数据对象
        /// </summary>
        /// <param name="p_created">是否新建</param>
        /// <param name="p_model">数据对象</param>
        public void SetModel(bool p_created, SysReport p_model)
        {
            created = p_created;
            model = p_model;
            Text = created ? "新建系统报表" : "修改系统报表";
            ckContinue.Visible = ckReserve.Visible = p_created;
        }

        /// <summary>
        /// 绑定数据到界面
        /// </summary>
        private void BindModel()
        {
            ReportService.Instance.SetReportPrinter(model);
            WinHelper.SetFormData(layoutPanel, model);
            editName.Select();
        }

        private void BindPrinterList()
        {
            editPrinter.Properties.Items.Add(string.Empty);
            editPrinter.Properties.Items.AddRange(PrinterSettings.InstalledPrinters);
        }

        //private void ValidReportPath()
        //{

        //}

        /// <summary>
        /// 保存数据对象
        /// </summary>
        private bool SaveModel()
        {
            WinHelper.GetFormData(layoutPanel, model);

            var isValid = ValidModel(model);
            if (!isValid) return false;

            var result = created ? ReportService.Instance.Insert(model) : ReportService.Instance.Update(model);

            if (result)
            {
                //设置报表
                SysRuntime.ReportPrinter[model.Code] = model.Printer;
                SysRuntime.ReportPrinterProvider.Save();
            }
            return result;
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private void FrmReportEdit_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        private void CkContinue_CheckedChanged(object sender, EventArgs e)
        {
            ckReserve.Enabled = ckContinue.Checked;
        }

        /// <summary>
        /// 保存事件
        /// </summary>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!SaveModel()) return;
            if (ckContinue.Checked) //连续保存
            {
                MessageBoxHelper.ShowMessage("保存成功");
                //localPath = string.Empty;
                var nmodel = ckReserve.Checked ? ReportService.Instance.Copy(model) : ReportService.Instance.New();
                SetModel(true, nmodel);
                BindModel();
            }
            else
            {
                ResultOk();
            }
        }

        /// <summary>
        /// 关闭事件
        /// </summary>
        private void btnClose_Click(object sender, EventArgs e)
        {
            //如果点关闭按钮后想刷新表格就用ResultOk,不刷新用ResultCancel
            ResultOk();
            //ResultCancel();
        }

        //private void FrmReportEdit_DragEnter(object sender, DragEventArgs e)
        //{
        //    e.Effect = DragDropEffects.Copy;
        //}

        //private void FrmReportEdit_DragDrop(object sender, DragEventArgs e)
        //{
        //    var files = (string[])(e.Data.GetData(DataFormats.FileDrop));
        //    if (files.Length > 0)
        //    {
        //        localPath = files[0];
        //        ValidReportPath();
        //    }
        //}

        //private void BtnSelectFile_Click(object sender, EventArgs e)
        //{
        //    if (openFileDialog1.ShowDialog() == DialogResult.OK)
        //    {
        //        localPath = openFileDialog1.FileName;
        //        ValidReportPath();
        //    }
        //}
    }
}